//
//  ExtUILabel.swift
//  koolcar
//
//  Created by huangweiqiang on 2022/6/20.
//

import UIKit
var label_tap_key: UnsafePointer<String>? = nil

extension UILabel {
    func addLabelTapGestureRecognizer(_ numberOfTaps: Int? = nil, aBlock: @escaping (Any) -> Void) {
        self.isUserInteractionEnabled = true
        let tempBlock = objc_getAssociatedObject(self, &label_tap_key) as? (Any) -> Void
        if tempBlock == nil {
            objc_setAssociatedObject(self, &label_tap_key, aBlock, .OBJC_ASSOCIATION_COPY_NONATOMIC)
        }else{
            return
        }
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapAction))
        tap.numberOfTapsRequired = numberOfTaps ?? 1
        self.addGestureRecognizer(tap)
    }
    
    @objc func tapAction(){
        let tempBlock = objc_getAssociatedObject(self, &label_tap_key) as? (Any) -> Void
        if tempBlock != nil {
            tempBlock?(self)
        }
    }
}
